package com.lonely.system.engine.log;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lonely.common.core.domain.TimeLine;
import com.lonely.system.domain.dto.log.GetLogTimeLineDto;
import com.lonely.system.domain.entity.SysBusinessLog;
import com.lonely.system.service.IBusinessLogService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : 黄志标
 * @date: 2025/9/19 17:13
 * @description: 抽象业务日志时间线服务
 */
public abstract class AbstractLogTimeLineService implements GetLogTimeLineInterface {

    @Autowired
    private IBusinessLogService businessLogService;

    /**
     * 获取业务日志时间线
     *
     * @param queryDto
     * @return
     */
    @Override
    public List<TimeLine> getLogTimeLine(GetLogTimeLineDto queryDto) {
        return this.businessLogService.list(new LambdaQueryWrapper<SysBusinessLog>()
                .eq(SysBusinessLog::getRefId, queryDto.getRefId())
                .eq(SysBusinessLog::getModuleName, queryDto.getModuleName())
                .orderByDesc(SysBusinessLog::getCreateTime, SysBusinessLog::getId)
        ).stream().map(currentLog -> {
            TimeLine timeLine = new TimeLine();
            timeLine.setType("info");
            timeLine.setContent(currentLog.getLogBody());
            timeLine.setTitle(currentLog.getLogTitle());
            timeLine.setTime(DateUtil.formatDateTime(currentLog.getCreateTime()));
            return timeLine;
        }).collect(Collectors.toList());
    }
}
